<?php

namespace App\Http\Controllers\Admin\Authorization;

use App\B2BProject\Admin\Authority\Rbac\Permission;
use App\B2BProject\Admin\Authority\Rbac\Role;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class RolePermissionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($roleId)
    {
        $permissions = Permission::all();

        $role = Role::findOrFail($roleId);

        $rolePermission = $role->permissions()->lists('id')->toArray();

        $data = [];

        foreach ($permissions as $permission) {
            $data[] = [
                'id'    => $permission->id,
                'text'  => $permission->display_name,
                'check' => in_array($permission->id, $rolePermission) ? 1 : 0,
            ];
        }

        return response()->json(['data' => [$data]]);
    }
}
